package thread;

public class Test14_ThreadGroup {
    public static void main(String[] args) {
        //创建任务对象
        TestThread task1 = new TestThread();
        TestThread task2 = new TestThread();

        ThreadGroup threadGroup = new ThreadGroup("新建线程组1");

        //创建两个线程，与两个任务绑定
        Thread t0 = new Thread(threadGroup,task1);
        Thread t1 = new Thread(threadGroup,task2);
        t0.start();
        t1.start();

        System.out.println("活动的线程数为："+threadGroup.activeCount());
        System.out.println("线程组名称为："+threadGroup.getName());
        //***线程组中断，则这个组中所有的线程全部中断
        threadGroup.interrupt();
    }
}

class TestThread implements Runnable{
    @Override
    public void run() {
        try {
            //判断当前线程的中断信号
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("线程名："+Thread.currentThread().getName());
                Thread.sleep(3000);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}